﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuantitativeIndicator.FundStkRatio
{

    class OutputSQL
    {
        public FundStkRatioDataModel FsrDB;
        public FundStkRatioCaculator FsrC;

        public OutputSQL()
        {
            this.FsrDB = new FundStkRatioDataModel();
            this.FsrC = new FundStkRatioCaculator();
        }

        public void OutputIndex(string startDate, string endDate,string type, string isIndex, string fundInvestType)
        {
            string lastDate = FsrDB.getDFZQ_FundHoldStockIndexLatestDate(type, isIndex, fundInvestType);
            string lastTradedDate=FsrDB.getStockLatestTradedDate();

            if (lastDate == null)
            {
                FsrDB.setBasePoint(type, isIndex, fundInvestType);
                startDate = "20050101".ToString();
            }
            else
            {
                startDate = lastDate;
            }

            List<string> tmpDateList = FsrDB.getTradingDateList(startDate, endDate);
            //List<string> newTmpDateList = new List<string>();

            foreach (string tmpDate in tmpDateList)
            {
                Console.WriteLine(tmpDate);
                double indexRtn=0;
                double lastIndexCP=0;
                double tmpDateIndexCP = 0;
                if((int.Parse(tmpDate)>int.Parse(startDate)) && (int.Parse(tmpDate)<=int.Parse(lastTradedDate)))
                {
                    lastIndexCP = FsrDB.getDFZQ_FundHoldStockIndexClosePrice(type, isIndex, fundInvestType);
                    indexRtn = FsrC.caculatePortfolioRtn(tmpDate,type,isIndex,fundInvestType);
                    tmpDateIndexCP = lastIndexCP * (1 + indexRtn);
                    FsrDB.insertDFZQ_FundHoldStockIndex(tmpDate, type, isIndex, fundInvestType, tmpDateIndexCP, lastIndexCP);
                }
            }

            
        }




    }
}
